// 从测试实用工具集中导入 `mount()` 方法
// 同时导入你要测试的组件
import { mount } from "@vue/test-utils";
import Counter from "./counter";
import "core-js/fn/promise";
// import QuantityComponent from '@/test/keydown.vue'

describe("Counter", () => {
  // 现在挂载组件，你便得到了这个包裹器
  const wrapper = mount(Counter);
  // 你可以通过 `wrapper.vm` 访问实际的 Vue 实例
  const vm = wrapper.vm;
  it("renders the correct markup", () => {
    expect(wrapper.html()).toContain('<span class="count">0</span>');
  });

  // 也便于检查已存在的元素
  it("has a button", () => {
    expect(wrapper.contains("button")).toBe(true);
  });

  // 模拟点击
  it("button should increment the count", () => {
    expect(wrapper.vm.count).toBe(0);
    const button = wrapper.find("button");
    button.trigger("click");
    expect(wrapper.vm.count).toBe(1);
  });
  test("null", () => {
    const n = null;
    expect(n).toBeNull();
    expect(n).toBeDefined();
    expect(n).not.toBeUndefined();
    expect(n).not.toBeTruthy();
    expect(n).toBeFalsy();
  });

  test("zero", () => {
    const z = 0;
    expect(z).not.toBeNull();
    expect(z).toBeDefined();
    expect(z).not.toBeUndefined();
    expect(z).not.toBeTruthy();
    expect(z).toBeFalsy();
  });

  function compileAndroidCode() {
    throw new Error("you are using the wrong JDK");
  }

  test("compiling android goes as expected", () => {
    expect(compileAndroidCode).toThrow();
    expect(compileAndroidCode).toThrow(Error);

    // You can also use the exact error message or a regexp
    expect(compileAndroidCode).toThrow("you are using the wrong JDK");
    expect(compileAndroidCode).toThrow(/JDK/);
  });

  // 测试异步调用
  function fetchData(resolve, reject) {
    setTimeout(() => {
      return resolve("peanut butter");
    }, 2000);
  }
  test("the data is peanut butter", () => {
    let fetch1 = new Promise(fetchData)
    return fetch1.then(data => {
      expect(data).toBe("peanut butter");
    });
  });
});
